
import Taro from '@tarojs/taro';
import { getAppData, authorization, setGlobalData, getUserId } from '@utils/tools'
import { RequestSetting, IQOOLOGIN } from '../interfaces/common'
import AppRoute from '@utils/route/index'
import { hosts } from '@utils/config'
import { reject } from 'lodash';

class Request {
    VIVORequest(api: string, params: any, setting?: RequestSetting) {
        let header = {}
        if (!!!Taro.getStorageSync('temporaryUserId')) {
            getUserId()
        }

        header = {
            Authorization: Taro.getStorageSync('Authorization') || '',
            "X-Visitor": Taro.getStorageSync('temporaryUserId'),
            // "X-Platform": 'mini',
            // SIGN: 'IQOO-HMAC-SHA256 ' + authorization(setting?.method || 'POST', '/api/' + api, params)
        }
        // const token = Taro.getStorageSync('token') || '';
        // const expiresAt = Taro.getStorageSync('tokenExpiresAt') || 0;
        // const now = Math.floor(Date.now() / 1000); // 当前时间戳（秒）  

        // if (token && expiresAt > now) {
        //     // token有效，可以继续执行后续操作  
        //     console.log('登录状态有效');
        //     // 这里可以执行你的关键操作  
        // } else {
        //     // token无效或已过期，提示用户重新登录  
        //     console.log('登录状态已过期，请重新登录');
        // }

        // // 在需要的时候调用checkLoginStatus函数  
        // checkLoginStatus();
        return new Promise((resolve, reject) => {
            if (!!setting?.loading) {
                Taro.showToast({
                    title: setting.loading,
                    icon: 'loading',
                    duration: 100000
                })
            }
            Taro.request({
                url: hosts + api,
                data: params,
                header: header,
                method: setting?.method || 'POST',
                success(result) {
                    console.info('响应数据',
                        {
                            'URL:': api,
                            'RESPONSE': result.data,
                            '传递参数': params,
                            Token: getAppData().Authorization
                        })
                    resolve(result.data)
                },

                fail(res) {
                    Taro.showToast({
                        title: '当前访问人数过多，请稍候访问',
                        icon: 'none',
                        duration: 3000
                    })
                    reject(res)
                },
            })
        })
    }
    //社区小程序上传文件
    IQOOUploadRequest(api: string, params: any, setting?: RequestSetting) {
        let header = {}

        if (!!!Taro.getStorageSync('temporaryUserId')) {
            getUserId()
        }
        if (!!getAppData().Authorization) {
            header = {
                Authorization: getAppData().Authorization.toString(),
                "X-Visitor": Taro.getStorageSync('temporaryUserId'),
                "X-Platform": 'mini'
            }
        }
        return new Promise((resolve, reject) => {
            Taro.uploadFile({
                url: hosts + api, //仅为示例，非真实的接口地址
                filePath: params.filePath,
                name: params.name,
                formData: params.formData,
                header: header,
                success(result) {
                    console.info('响应数据',
                        {
                            'URL:': api,
                            'RESPONSE': result.data
                        })
                    resolve(result.data)
                },
                fail(res) {
                    reject(res)
                },
            })
        })
    }
    //社区小程序登录
    VIVOLogin = (encryptedData, iv) => {
        let _this = this
        return new Promise((resolve, reject) => {
            Taro.login({
                success(result) {
                    if (result.code) {
                        let FromData = {}
                        FromData.code = result.code
                        FromData.encryptedData = encryptedData
                        FromData.iv = iv
                        // return
                        _this.VIVORequest('v1/users/yiye/mini', FromData).then((res: any) => {
                            // Taro.setStorageSync('randomNum', res?.Data?.randomNum)
                            resolve(res)
                        }).catch((err) => {
                            // Taro.setStorageSync('randomNum', err?.data.Data?.randomNum)
                            reject(err)
                        })
                        // }
                        // })
                    }
                },
            })
        })
    }
    // 获取地址接口
    handleOrigin = (code) => {
        let _this = this
        return new Promise((resolve, reject) => {
            _this.VIVORequest('/api/v1/location', { parent_code: code }, { method: 'GET' }).then((response: any) => {
                console.log(response, '获取收货地址')
                resolve(response?.data?.item)
            }).catch((err) => {
                reject(err)
            })
        })
    }
}

const JXPPRequest = new Request();
export default JXPPRequest;
